package io.lckgroup.modules.lck.controller.app;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import io.lckgroup.common.utils.R;
import io.lckgroup.common.validator.ValidatorUtils;
import io.lckgroup.modules.lck.annotation.AppLogin;
import io.lckgroup.modules.lck.annotation.AppLoginUser;
import io.lckgroup.modules.lck.entity.MerchantUserEntity;
import io.lckgroup.modules.lck.entity.ShopMerchantInfoEntity;
import io.lckgroup.modules.lck.form.AppLoginForm;
import io.lckgroup.modules.lck.service.MerchantUserService;
import io.lckgroup.modules.lck.service.ShopMerchantInfoService;
import io.lckgroup.modules.lck.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * APP登录授权
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("/app")
@Api("APP登录接口")
public class AppLoginController {
    @Autowired
    private MerchantUserService merchantUserService;

    @Autowired
    private JwtUtils jwtUtils;

    @Autowired
    private ShopMerchantInfoService shopMerchantInfoService;

    /**
     * 登录
     */
    @PostMapping("login")
    @ApiOperation("登录")
    public R login(@RequestBody AppLoginForm form) {


        //表单校验
        ValidatorUtils.validateEntity(form);
        //用户登录
        R r = merchantUserService.login(form);
        if (null == r.get("user")) {
            return r;
        } else {
            MerchantUserEntity mue = (MerchantUserEntity) (r.get("user"));

            ShopMerchantInfoEntity shopMerchantInfoEntity = this.getMerchantInfoByUserId(mue.getUserId());
            //生成token
            String token = jwtUtils.generateToken(shopMerchantInfoEntity.getId());

            String clientid = form.getClientid();
            if (StringUtils.isNotEmpty(clientid)) {
                ShopMerchantInfoEntity entity = new ShopMerchantInfoEntity();
                entity.setUserId(mue.getUserId());
                Wrapper<ShopMerchantInfoEntity> updateWrapper =
                        new UpdateWrapper<ShopMerchantInfoEntity>(entity);
                ShopMerchantInfoEntity entity2 = new ShopMerchantInfoEntity();
                entity2.setClientId(clientid);
                shopMerchantInfoService.update(entity2, updateWrapper);
            }

            return r.put("token", token).put("expire", jwtUtils.getExpire());
        }
    }


    /**
     * APP端修改密码方法
     *
     * @param sm
     * @return 返回一个map对象
     */
    @AppLogin
    @PostMapping("appUpdatePassword")
    @ApiOperation("app端修改密码")
    public R appUpdatePassword(@RequestBody Map<String, Object> params, @AppLoginUser ShopMerchantInfoEntity sm) {
        params.put("userId", sm.getUserId());
        Map<String, Object> map;
        map = merchantUserService.updatePassword(params);
        return R.ok(map);
    }

    /**
     * 通过用户id获取商家信息
     *
     * @param userId
     * @return ShopMerchantInfoEntity
     */
    private ShopMerchantInfoEntity getMerchantInfoByUserId(Long userId) {
        Wrapper<ShopMerchantInfoEntity> queryWrapper =
                new QueryWrapper<ShopMerchantInfoEntity>()
                        .eq("user_id", userId);
        ShopMerchantInfoEntity merchant = shopMerchantInfoService.getOne(queryWrapper);
        return merchant;
    }

}
